package cn.edu.nuc.weiboclient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oauth.signpost.OAuth;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Enter extends Activity{
	private Button sendSendButton = null;
	private EditText sendEditText = null;
	final String TAG = getClass().getName();
	private SharedPreferences prefs;
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem followsItem = menu.add(Menu.NONE, 1, 1, "Friends");
		MenuItem mainItem = menu.add(Menu.NONE, 0, 0, "ME");
		MenuItem friendsItem = menu.add(Menu.NONE, 2, 2, "Funs");
		MenuItem homepageItem = menu.add(Menu.NONE, 3, 3, "@ME");
		MenuItem otherUsersItem = menu.add(Menu.NONE,4, 4, "LoginInterface");
		MenuItem usermentionedItem = menu.add(Menu.NONE, 5, 5, "Exit");
		
		followsItem.setIcon(android.R.drawable.ic_menu_send);
		friendsItem.setIcon(android.R.drawable.ic_menu_more);
		homepageItem.setIcon(android.R.drawable.ic_menu_send);
		otherUsersItem.setIcon(android.R.drawable.ic_menu_more);
		usermentionedItem.setIcon(android.R.drawable.ic_menu_send);
		mainItem.setIcon(android.R.drawable.ic_media_rew);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if(item.getItemId() == 5){
			finish();
			System.exit(0);
		}
		else if(item.getItemId() == 1){
			Intent intent = new Intent();
			intent.setClass(this,FriendsTimeLineActivity.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 2){
			Intent intent = new Intent();
			intent.setClass(this, FollowsTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 3){
			Intent intent = new Intent();
			intent.setClass(this, UsermentionedTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 4){
			Intent intent = new Intent();
			intent.setClass(this,WeiboClientActivity.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 0){
			Intent intent = new Intent();
			intent.setClass(this,MyPostTimeLine.class);
			startActivity(intent);
			finish();
		}
		return super.onMenuItemSelected(featureId, item);
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.send);
		
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		sendSendButton = (Button)findViewById(R.id.sendSendButtonId);
		sendEditText = (EditText)findViewById(R.id.sendEditTextId);
		sendSendButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String s1 = sendEditText.getText().toString();
				if(s1 != ""){
					Map<String,String> map = new HashMap<String,String>();
					map.put("content",s1);
					map.put("clientip", "127.0.0.1");
					map.put("format", "json");
					List<String> decodeNames = new ArrayList<String>();
					decodeNames.add("oauth_signature");
					WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, prefs.getString(OAuth.OAUTH_TOKEN, ""), prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""));
					String result = weiBoClient.doPost(Constants.WeiBoApi.ADD,map,decodeNames);
					System.out.println(result);
					Intent intent = new Intent(); 
					intent.setClass(Enter.this,HomepageTimeLine.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}
				else if(s1 == ""){
					Intent intent = new Intent(); 
					intent.setClass(Enter.this,HomepageTimeLine.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
					finish();
				}
			}
		});
	}

}
